package com.permission.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.permission.common.JsonData;
import com.permission.dto.DepLevelDto;
import com.permission.param.DeptParam;
import com.permission.service.SysDeptService;
import com.permission.service.SysTreeService;

import lombok.extern.slf4j.Slf4j;

/**
 * 部门Controller
 * @author jing
 */
@Controller
@RequestMapping("/sys/dept")
@Slf4j
public class SysDeptController {
	
	@Autowired
	private SysDeptService sysDeptService;
	@Autowired
	private SysTreeService sysTreeService;
	
	/**
	 * 返回页面
	 */
	@RequestMapping("/dept.page")
	public ModelAndView page(){
		return new ModelAndView("dept");
	}
	
	/**
     * 获取部门树
     * @return
     */
    @RequestMapping("/tree.json")
    @ResponseBody
    public JsonData tree() {
        List<DepLevelDto> dtoList = sysTreeService.getDepTree();
        return JsonData.success(dtoList);
    }
	
	
	/**
	 * 增加部门
	 * @param dep
	 * @return
	 */
	@RequestMapping("/save.json")
	@ResponseBody
	public JsonData saveDept(DeptParam dep){
		sysDeptService.save(dep);
		return JsonData.success();
	}
	
	/**
	 * 删除部门
	 * @param dep
	 * @return
	 */
	@RequestMapping("/delete.json")
	@ResponseBody
	public JsonData deleteDept(Integer deptId){
		sysDeptService.delete(deptId);
		return JsonData.success();
	}
	
	/**
	 * 更新部门
	 * @param dep
	 * @return
	 */
	@RequestMapping("/update.json")
	@ResponseBody
	public JsonData updateDept(DeptParam dep){
		sysDeptService.update(dep);
		return JsonData.success();
	}
}
